package io.github.libkodi.session;

import java.util.HashMap;

public class DataItem {
	private long timestamp = 0;
	private Object data = null;
	private int expire = 0;

	public DataItem(Object obj) {
		this.data  = obj;
		this.renew();
	}
	
	public DataItem renew() {
		timestamp = System.currentTimeMillis();
		return this;
	}

	public Object getObject() {
		return this.data;
	}
	
	@SuppressWarnings("unchecked")
	public HashMap<String, Object> getMap() {
		return (HashMap<String, Object>) this.data;
	}

	public DataItem setObject(Object obj) {
		this.data = obj;
		return this;
	}
	
	public DataItem setExpire(int seconds) {
		this.expire = seconds;
		return this;
	}
	
	public boolean isExpired() {
		return timestamp > 0 && expire > 0 && (System.currentTimeMillis() - timestamp) / 1000 >= expire;
	}
}
